var express = require('express')
var router = express.Router()
const { getALL, getOne, exec } = require('../../db')

// 获取所有用户
router.get('/', async (req, res) => {
  let sql = `select * from user`
  const data = await getALL(sql)
  res.send({
    code: '200',
    msg: '查询成功',
    result: data,
  })
})
// 获取普通用户
router.get('/normal', async (req, res) => {
  let sql = `select * from user where user_type = 'normal' `
  try {
    const data = await getALL(sql)
    res.send({
      code: '200',
      msg: '查询成功',
      result: data,
    })
  } catch {
    res.send({
      code: '404',
      msg: '查询失败',
    })
  }
})
// 获取交管账户信息
router.get('/traffic', async (req, res) => {
  let sql = `select * from user where user_type = 'traffic'`
  try {
    const data = await getALL(sql)
    res.send({
      code: '200',
      msg: '查询成功',
      result: data,
    })
  } catch {
    res.send({
      code: '404',
      msg: '查询失败',
    })
  }
})
// 搜索用户
router.get('/search', async (req, res) => {
  const { username, type } = req.query
  let sql = `select * from user where username like '%${username}%' and user_type = '${type}'`
  try {
    const data = await getALL(sql)
    res.send({
      code: '200',
      msg: '搜索成功',
      result: data,
    })
  } catch {
    res.send({
      code: '404',
      msg: '搜索失败',
    })
  }
})
// 修改用户状态
router.post('/status', async (req, res) => {
  const { status, id } = req.body
  console.log(status)
  let sql = `update user set user_onlinestatus = '${status}' where id = ${id}`
  console.log(sql)
  try {
    await exec(sql)
    res.send({
      code: '200',
      msg: '修改成功',
    })
  } catch {
    res.send({
      code: '400',
      msg: '修改失败',
    })
  }
})
// 删除用户
router.post('/delete', async (req, res) => {
  const { id } = req.query
  let sql = `delete from user where id = ${id}`
  try {
    await exec(sql)
    res.send({
      code: '200',
      msg: '删除成功',
    })
  } catch {
    res.send({
      code: '400',
      msg: '删除失败',
    })
  }
})
// 改变用户等级
router.post('/changelevel', async (req, res) => {
  const { id, type } = req.body
  let sql = `update user set user_type = '${type}' where id = ${id} `
  try {
    await exec(sql)
    res.send({
      code: '200',
      msg: '修改成功',
    })
  } catch {
    res.send({
      code: '400',
      msg: '修改失败',
    })
  }
})
// 修改用户名和密码
router.post('/changeuser', async (req, res) => {
  const { id, username, password } = req.body
  let sql = `update user set username='${username}',password='${password}' where id = ${id} `
  try {
    await exec(sql)
    res.send({
      code: '200',
      msg: '修改成功',
    })
  } catch {
    res.send({
      code: '400',
      msg: '修改失败',
    })
  }
})
// 删除选中
router.post('/delselected', async (req, res) => {
  console.log('2')
  const { val } = req.body
  let sql = `delete from user where id in (${val})`
  try {
    await exec(sql)
    res.send({
      code: '200',
      msg: '删除成功',
    })
  } catch {
    res.send({
      code: '400',
      msg: '删除失败',
    })
  }
})
// 新增用户
router.post('/adduser', async (req, res) => {
  const { username, password, type } = req.body
  let sql = `insert into user (username,password,user_type) values('${username}','${password}','${type}')`
  try {
    await exec(sql)
    res.send({
      code: '200',
      msg: '新增用户成功',
    })
  } catch {
    res.send({
      code: '400',
      msg: '新增用户失败',
    })
  }
})

module.exports = router
